Logging in Python

Generating logs from a python program

Created: August 21st 2019
Updated: December 3th 2019
Connect with us on
image
image

Logging is the act of generating logs from a program. These are typically stored in a file or sent to a terminal to give developers a good idea of what's going on in their program at any given time.

Definitions

Some terms to help you navigate the post

Logging

Logging is the act of generating logs from a program. The logging module is the standard way of doing this in python.

You can see in the example below the simplest way of setting up a logger to log to example.log:

import logging # Module that allows you to create logs

logging.basicConfig(filename='example.log', # Specifying a filename automatically puts all logs in the filename path
    filemode='w', # Allows you to specify filemode; SEE: https://docs.python.org/3.7/library/functions.html#open
    level=logging.DEBUG) # Defines what type of logs should show up; SEE: https://docs.python.org/3/howto/logging.html#logging-levels
logging.info("Hello World!")

Usage

In this repo you can see the demo code and actually run it by running python logging_demo.py or python3 logging_demo.py.

Real World Applications

Logging is incredibly useful for debugging down the road, especially if you are running a very complicated app with many modules and files as hand debugging becomes genuinely impossible sometimes. It also helps you to understand what went wrong in edge cases when end users don't necessarily remember exactly what they did to break something.

Additional Info

Files

logging_demo.py

import logging # Module that allows you to create logs; Logs are very helpful for down the road debugging
import datetime # Used in formatting strings to identify date and time

def print_num():
    """Takes user input, and if it's an int or float prints it."""
    logging.debug("Starting print_int") # Only gets logged if loggers level is DEBUG (10) or below
    try:
        logging.info("Prompting for input") # Only gets logged if loggers level is INFO (20) or below
        user_input = input("Enter a number:")
        user_input = eval(user_input) # Convert input to an int or float
        logging.info(f"User entered int or float: {user_input}")
        print(f"User entered int or float: {user_input}")
    except: # If the user enters something other than an int or float
        logging.warning(f"User didn't enter a number, they entered {user_input}") # Only gets logged if loggers level is WARNING (30) or below

if __name__ == "__main__":
    # For backwards compatability the logging module forces % formatting for predefined values
    # SEE: For a full list of variables visit: https://docs.python.org/3/library/logging.html#logrecord-attributes
    LOG_FORMAT = "{0} | %(levelname)s | %(module)s | : %(message)s".format(datetime.datetime.now().time())
    # Instantiate a logger in the simplest way possible
    logging.basicConfig(format=LOG_FORMAT, # Pass the log format defined above to the logger
        filename='example.log', # Specifying a filename automatically puts all logs in the filename path
        filemode='w', # Allows you to specify filemode; SEE: https://docs.python.org/3.7/library/functions.html#open
        level=logging.DEBUG) # Defines what type of logs should show up; SEE: https://docs.python.org/3/howto/logging.html#logging-levels
    print_num()

Thank you for your support!

Be sure to share the post if it was helpful!